package cz.farin.mandelinka.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import cz.farin.mandelinka.BugHandle;
import cz.farin.mandelinka.Simulation;

public class ControlPanel extends JPanel {
	
	private static Font nameFont = new Font("Helvecia", Font.BOLD, 14);
	private static Font scoreFont = new Font("Helvecia", Font.BOLD, 30);
	
	//private Simulation sim;
	private BugPanel[] bugPanels;
	
	public ControlPanel(Simulation sim) {
		//this.sim = sim;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBackground(Color.WHITE);
		
		bugPanels = new BugPanel[sim.getAllBugs().size()];
		for(int i = 0; i < bugPanels.length; i++) {
			BugHandle bh = sim.getAllBugs().get(i);
			bugPanels[i] = new BugPanel(bh);
			add(bugPanels[i]);			
		}		
		
		//setLayout(new GridLayout(bugPanels.length, 1));		
		//setMinimumSize(new Dimension(BugPanel.WIDTH,500));
		
	}
	
	
	class BugPanel extends JPanel {
		
		public static final int PANEL_WIDTH = 200;
		public static final int PANEL_HEIGHT = 80;
		
		private BugHandle bh;
		
		public BugPanel(BugHandle bh) {
			this.bh = bh;
			Dimension size = new Dimension(PANEL_WIDTH, PANEL_HEIGHT);			
			setMinimumSize(size);
			setMaximumSize(size);
			setPreferredSize(size);
		}
		
		@Override
		public void paintComponent(Graphics g) {
			//mozna by to chtelo refactoring
			super.paintComponent(g);
								
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			
			if (! bh.isAlive()) {
				g2.setColor(Color.LIGHT_GRAY);
			} else {
				g2.setColor(Color.WHITE);
			}
			g2.fillRect(0, 0, PANEL_WIDTH, PANEL_HEIGHT);
			
			g2.setColor(Color.BLACK);
			g2.drawRect(1, 1, PANEL_WIDTH-3, PANEL_HEIGHT-3);
			
			g2.setFont(nameFont);
			g2.drawString(bh.getBug().getName(),0,40);
			g2.setFont(scoreFont);
			g2.drawString(bh.getScore() + "",150,40);
			
		}
	}
	
	

}
